package com.cardbaobao.cardbabyclient.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.model.UserBindCards;
import com.cardbaobao.cardbabyclient.ui.adapter.base.CommonAdapter;
import com.cardbaobao.cardbabyclient.ui.adapter.base.ViewHolder;
import com.cardbaobao.cardbabyclient.utils.ImageLoaderUtil;
import com.cardbaobao.cardbabyclient.utils.URLUtil;

import java.util.List;
import java.util.Locale;

/**
 * Created by Jiangzx on 2017/9/20 19:13.
 */

public class CreditLimitQuotaAdapter extends BaseAdapter {
    public static final int TYPE_ITEM_NORMAL=1;//信用卡项
    public static final int TYPE_ADD_CARD=2;//添加信用卡项

    private Context mContext;
    private List<TypeCreditCard> mData;


    private int selected=-1;
    private double inputAmount;
    /**
     *
     * @param context context
     * @param data 信用卡集合
     * @param inputAmount 收款金额
     */
    public CreditLimitQuotaAdapter(Context context, List<TypeCreditCard> data,double inputAmount) {
        this.mContext=context;
        this.mData=data;
        this.inputAmount=inputAmount;
    }

    private class Holder{
        ImageView ivLogo;
        ImageView ivSelect;
        TextView tvCard;
        TextView tvQuota;
        Holder(View convertView){
            ivLogo= (ImageView) convertView.findViewById(R.id.iv_bank_logo);
            ivSelect= (ImageView) convertView.findViewById(R.id.iv_selected);
            tvCard= (TextView) convertView.findViewById(R.id.tv_card);
            tvQuota= (TextView) convertView.findViewById(R.id.tv_card_limit);
        }
    }

    private double getCardQuota(UserBindCards.CreditCard creditCard){
        double limitQuotaD;
        try{
            limitQuotaD=Double.parseDouble(creditCard.getQuota());
        }catch (Exception e){
            e.printStackTrace();
            limitQuotaD=0.00D;
        }
        return limitQuotaD;
    }

    public boolean canSelect(int position){
        return getCardQuota(mData.get(position).getCard())>=inputAmount;
    }

    public void setSelected(int position){
        this.selected=position;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mData==null?0:mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData==null?null:mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return TYPE_ADD_CARD+TYPE_ITEM_NORMAL;
    }

    @Override
    public int getItemViewType(int position) {
        if(mData!=null){
            TypeCreditCard card=mData.get(position);
            return card.getType();
        }
        return super.getItemViewType(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TypeCreditCard typeData=mData.get(position);
        int type=typeData.getType();
        if(type==TYPE_ITEM_NORMAL){
            Holder holder;
            //信用卡item
            if(convertView==null){
                convertView= LayoutInflater.
                        from(mContext).
                        inflate(R.layout.item_choose_bank_card,parent,false);
                holder=new Holder(convertView);
                convertView.setTag(R.layout.item_choose_bank_card,holder);
            }else{
                holder= (Holder) convertView.getTag(R.layout.item_choose_bank_card);
            }
            UserBindCards.CreditCard item=typeData.getCard();
            ImageLoaderUtil.displayImage(URLUtil.CBB_IMG_URL + item.getPicture(),
                    holder.ivLogo);
            String cardNumber = item.getCredit_card_no();
            holder.tvCard.setText(item.getCredit_card_name() +
                    "信用卡 (" +
                    cardNumber.substring(cardNumber.length() - 4) + ")");
            holder.tvQuota.setText("银行单笔限额" +
                    (String.format(Locale.CHINESE, "%.2f", getCardQuota(item))) +
                    "元");
            if (canSelect(position)) {
                convertView.setAlpha(1.0f);
            } else {
                convertView.setAlpha(0.5f);
            }
        }else if(type==TYPE_ADD_CARD){
            //添加信用卡
            if(convertView==null){
                convertView=LayoutInflater.
                        from(mContext).
                        inflate(R.layout.item_add_credit_card,parent,false);
            }
        }
        return convertView;
    }

    public static class TypeCreditCard{
        private int type;
        private UserBindCards.CreditCard card;

        public TypeCreditCard(int type, UserBindCards.CreditCard card) {
            this.type = type;
            this.card = card;
        }

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }

        public UserBindCards.CreditCard getCard() {
            return card;
        }

        public void setCard(UserBindCards.CreditCard card) {
            this.card = card;
        }
    }
}
